package com.rongma.waterdrop.manager.bmobmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rongma.waterdrop.db.WaterDropHelper;
import com.rongma.waterdrop.global.TimerStatus;
import com.rongma.waterdrop.model.TimerEvent;

import java.util.ArrayList;

/**
 * Created by RongMa on 16/6/14.
 */

public class TimerEventManager
{
    private static final String DB_NAME = "timerevent";
    private static final String COLUMN_ICONBG = "iconBg";
    private static final String COLUMN_ICONSRC = "iconSrc";
    private static final String COLUMN_TITLE = "title";
    private static final String COLUMN_CONTENT = "content";
    private static final String COLUMN_TIME = "time";
    private static final String COLUMN_ICONCOLOR = "iconColor";
    private static final String COLUMN_STATUS = "status";
    private static final String COLUMN_UPLOAD = "upload";

    private static TimerEventManager manager = new TimerEventManager();
    private static WaterDropHelper waterDropHelper;

    private TimerEventManager()
    {
    }

    public static TimerEventManager getManager(Context context)
    {
        waterDropHelper = new WaterDropHelper(context);
        return manager;
    }

    //批量添加数据
    public void insertTimerList(ArrayList<TimerEvent> events)
    {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        ContentValues values = new ContentValues();
        try
        {
            for(TimerEvent event : events)
            {
                values.put(COLUMN_CONTENT,event.getContent());
                values.put(COLUMN_ICONBG,event.getIconBg());
                values.put(COLUMN_ICONCOLOR,event.getIconColor());
                values.put(COLUMN_ICONSRC,event.getIconSrc());
                values.put(COLUMN_TIME,event.getTime());
                values.put(COLUMN_TITLE,event.getTitle());
                values.put(COLUMN_STATUS,TimerStatus.STOP);
                values.put(COLUMN_UPLOAD,0);
                sdb.insert(DB_NAME,null,values);
                values.clear();
            }
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    public void updateTimer(TimerEvent event)
    {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        ContentValues values = new ContentValues();
        try
        {
            values.put(COLUMN_CONTENT,event.getContent());
            values.put(COLUMN_ICONBG,event.getIconBg());
            values.put(COLUMN_ICONCOLOR,event.getIconColor());
            values.put(COLUMN_ICONSRC,event.getIconSrc());
            values.put(COLUMN_TIME,event.getTime());
            values.put(COLUMN_TITLE,event.getTitle());
            values.put(COLUMN_STATUS,event.getStatus());
            sdb.update(DB_NAME,values,COLUMN_TITLE+"=?",new String[]{event.getTitle()});
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    //批量获取数据
    public ArrayList<TimerEvent> queryAllTimer()
    {
        SQLiteDatabase sdb = waterDropHelper.getReadableDatabase();
        Cursor cursor = sdb.query(DB_NAME,null,null,null,null,null,null);
        ArrayList<TimerEvent> events = new ArrayList<TimerEvent>();
        if(cursor.getCount() == 0)
        {
            return events;
        }
        cursor.moveToFirst();
        try
        {
            do
            {
                TimerEvent event = new TimerEvent();
                event.setContent(cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));
                event.setIconBg(cursor.getLong(cursor.getColumnIndex(COLUMN_ICONBG)));
                event.setIconColor(cursor.getLong(cursor.getColumnIndex(COLUMN_ICONCOLOR)));
                event.setIconSrc(cursor.getLong(cursor.getColumnIndex(COLUMN_ICONSRC)));
                event.setTime(cursor.getLong(cursor.getColumnIndex(COLUMN_TIME)));
                event.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)));
                event.setStatus(cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS)));
                events.add(event);
            }
            while(cursor.moveToNext());
        }
        finally
        {
            cursor.close();
            sdb.close();
        }
        return events;
    }


    //将处于开启状态的Event数据改为Stop,并返回修改的Event
    public TimerEvent setAllEventStop(ArrayList<TimerEvent> timerEvents)
    {
        TimerEvent timerEvent = null;
        for(TimerEvent event : timerEvents)
        {
            if(event.getStatus() == TimerStatus.START)
            {
                event.setStatus(TimerStatus.STOP);
                updateTimer(event);
                timerEvent = event;
            }
        }
        return timerEvent;
    }



}
